Imports System.Runtime.Serialization.Formatters.Binary
Imports OpenArchive.Document.Components.Data
Imports OpenArchive.IO

Namespace OpenArchive
	Namespace Formatters.Binary
		Public MustInherit Class BinaryComponentFormatter
			Implements IComponentFormatter
            Private m_writer As ArchiveBinaryWriter
            Private m_reader As ArchiveBinaryReader
			Protected WithEvents StartSerialization As Object
			Protected WithEvents FinishedSerialization As Object
            Public MustOverride Function IsResponsible(ByVal Type As Type, ByVal Version As Version) As Boolean Implements IComponentFormatter.IsResponsible
            Public MustOverride Function Deserialize(ByVal serializationStream As Global.System.IO.Stream) As ComponentData Implements IComponentFormatter.Deserialize

            Public Overridable Sub Serialize(ByVal serializationStream As Global.System.IO.Stream, ByVal Datacomponent As ComponentData) Implements IComponentFormatter.Serialize
                If m_writer Is Nothing Then
                    m_writer = New ArchiveBinaryWriter(serializationStream, New Text.ASCIIEncoding)
                End If
                m_writer.Write(Datacomponent.GetType.Name)
                m_writer.Write(Datacomponent.Version)
                ' ToDo Write Digital Signature, Use ToBinary Function
            End Sub

            Protected Function CheckResponsibility(ByVal Instance As ComponentData, ByVal Type As Type, ByVal Version As Version) As Boolean
                If Instance.GetType.Equals(Type) Then
                    ' Check Version
                    Return Instance.Version >= Version
                End If
            End Function
            Protected Property Writer() As ArchiveBinaryWriter
                Get
                    Return m_writer
                End Get
                Set(ByVal value As ArchiveBinaryWriter)
                    m_writer = value
                End Set
            End Property
            Protected Property Reader() As ArchiveBinaryReader
                Get
                    Return m_reader
                End Get
                Set(ByVal value As ArchiveBinaryReader)
                    m_reader = value
                End Set
            End Property
        End Class
	End Namespace
End Namespace



